#!/bin/bash

# API测试脚本
BASE_URL="http://localhost:3912/api/v1"

echo "================================"
echo "  清华美食地图 API 测试"
echo "================================"
echo ""

# 1. 管理员登录
echo "1️⃣  测试管理员登录..."
ADMIN_LOGIN_RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/admin/login" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "admin123"
  }')

echo "响应: $ADMIN_LOGIN_RESPONSE"
ADMIN_TOKEN=$(echo $ADMIN_LOGIN_RESPONSE | grep -o '"token":"[^"]*' | cut -d'"' -f4)

if [ -z "$ADMIN_TOKEN" ]; then
  echo "❌ 管理员登录失败"
  exit 1
fi

echo "✅ 管理员登录成功"
echo "Token: ${ADMIN_TOKEN:0:50}..."
echo ""

# 2. 获取地点列表
echo "2️⃣  测试获取地点列表..."
curl -s -X GET "${BASE_URL}/locations?status=APPROVED" \
  -H "Content-Type: application/json" | jq '.'
echo ""

# 3. 获取地点详情（观畴园食堂）
echo "3️⃣  测试获取地点详情 (ID: 1)..."
curl -s -X GET "${BASE_URL}/locations/1" \
  -H "Content-Type: application/json" | jq '.'
echo ""

# 4. 创建新地点（管理员）
echo "4️⃣  测试创建地点（管理员）..."
CREATE_LOCATION_RESPONSE=$(curl -s -X POST "${BASE_URL}/locations" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}" \
  -d '{
    "name": "学堂餐厅",
    "coordinateX": 50.5,
    "coordinateY": -75.3
  }')

echo "响应: $CREATE_LOCATION_RESPONSE" | jq '.'
NEW_LOCATION_ID=$(echo $CREATE_LOCATION_RESPONSE | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
echo "✅ 新地点ID: $NEW_LOCATION_ID"
echo ""

# 5. 为地点评分
echo "5️⃣  测试为地点评分..."
curl -s -X POST "${BASE_URL}/locations/1/ratings" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}" \
  -d '{
    "score": 4.5
  }' | jq '.'
echo ""

# 6. 获取我的评分
echo "6️⃣  测试获取我的评分..."
curl -s -X GET "${BASE_URL}/locations/1/ratings/my" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}" | jq '.'
echo ""

# 7. 添加推荐菜品
echo "7️⃣  测试添加推荐菜品..."
curl -s -X POST "${BASE_URL}/locations/1/dishes" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}" \
  -d '{
    "dishName": "酸辣土豆丝",
    "floor": "一楼",
    "price": 8.5
  }' | jq '.'
echo ""

# 8. 获取推荐菜品列表
echo "8️⃣  测试获取推荐菜品列表..."
curl -s -X GET "${BASE_URL}/locations/1/dishes" \
  -H "Content-Type: application/json" | jq '.'
echo ""

# 9. 更新拥堵程度
echo "9️⃣  测试更新拥堵程度..."
curl -s -X PUT "${BASE_URL}/locations/1/congestion" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}" \
  -d '{
    "timePeriod": "BREAKFAST",
    "level": 3
  }' | jq '.'
echo ""

# 10. 批量更新拥堵程度
echo "🔟 测试批量更新拥堵程度..."
curl -s -X PUT "${BASE_URL}/locations/2/congestion/batch" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}" \
  -d '{
    "congestions": [
      {"timePeriod": "BREAKFAST", "level": 2},
      {"timePeriod": "LUNCH", "level": 4},
      {"timePeriod": "DINNER", "level": 5}
    ]
  }' | jq '.'
echo ""

# 11. 获取拥堵程度
echo "1️⃣1️⃣  测试获取拥堵程度..."
curl -s -X GET "${BASE_URL}/locations/2/congestion" \
  -H "Content-Type: application/json" | jq '.'
echo ""

# 12. 审核地点（通过）
echo "1️⃣2️⃣  测试审核地点（通过）..."
curl -s -X PUT "${BASE_URL}/locations/3/approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}" | jq '.'
echo ""

# 13. 获取待审核地点列表
echo "1️⃣3️⃣  测试获取待审核地点列表..."
curl -s -X GET "${BASE_URL}/locations?status=PENDING" \
  -H "Content-Type: application/json" | jq '.'
echo ""

# 14. 删除地点
echo "1️⃣4️⃣  测试删除地点..."
if [ ! -z "$NEW_LOCATION_ID" ]; then
  curl -s -X DELETE "${BASE_URL}/locations/${NEW_LOCATION_ID}" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${ADMIN_TOKEN}" | jq '.'
  echo ""
fi

echo "================================"
echo "  ✅ 所有API测试完成！"
echo "================================"
